package com.frame.demo.rest;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.frame.demo.exception.DDYCBizException;
import com.frame.demo.exception.XKTickOutException;

import java.io.IOException;

public class ItemTypeAdapterFactory implements TypeAdapterFactory {
    public static final int ERROR_CODE_TICK_OUT_1 = 1003;
    public static final int ERROR_CODE_TICK_OUT_2 = 1004;
    public static final int ERROR_CODE_TICK_OUT_3 = 3001;

    public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {

        final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
        final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);

        return new TypeAdapter<T>() {

            public void write(JsonWriter out, T value) throws IOException {
                delegate.write(out, value);
            }


            public T read(JsonReader in) throws IOException {

                JsonElement jsonElement = elementAdapter.read(in);
                if (jsonElement.isJsonObject()) {
                    JsonObject jsonObject = jsonElement.getAsJsonObject();
                    if (jsonObject.has("code")) {
                        if (jsonObject.has("data")) { //code和data在同一层，认为是第一层
                            if (jsonObject.get("code").getAsInt() != 0) {
                                String msg = null;
                                if (jsonObject.has("msg")) {
                                    msg = jsonObject.get("msg").getAsString();
                                }
                                int code = jsonObject.get("code").getAsInt();
                                if (isTickOut(code)) {
                                    throw new XKTickOutException(msg);
                                } else {
                                    throw new DDYCBizException(msg, code);
                                }
                            }
                            jsonElement = jsonObject.get("data");
                        } else { //有code无data
                            if (jsonObject.has("msg")) {
                                String msg = jsonObject.get("msg").getAsString();
                                if (!TextUtils.isEmpty(msg)) {//有msg，并且不为空
                                    int code = jsonObject.get("code").getAsInt();
                                    if (code > 0) {
                                        if (isTickOut(code)) {
                                            throw new XKTickOutException(msg);
                                        } else {
                                            throw new DDYCBizException(msg, code);
                                        }
                                    }
                                }
                            } else {
                                // TODO: 2017/9/19 做点什么
                            }
                        }
                    } else {
                        if (jsonObject.has("data")) { // 有data无code，认为data是T中的字段
                            // TODO: 2017/9/19 再做点什么
                        } else {

                        }
                    }


//                    if (jsonObject.has("code") && jsonObject.get("code").getAsInt() != 0) {
//                        String msg = null;
//                        if (jsonObject.has("msg")) {
//                            msg = jsonObject.get("msg").getAsString();
//                        }
//                        int code = 0;
//                        if ( jsonObject.has("code")) {
//                            code = jsonObject.get("code").getAsInt();
//                        }
//
//                        if ( isTickOut(code)) {
//                            throw new XKTickOutException(msg);
//                        }  else {
//                            throw new DDYCBizException(msg, code);
//                        }
//                    }
//
//                    if (jsonObject.has("data")) {
//                        jsonElement = jsonObject.get("data");
//                    }
                }
                return delegate.fromJsonTree(jsonElement);
            }

            private boolean isTickOut(int code) {
                if (code == ERROR_CODE_TICK_OUT_1 ||
                        code == ERROR_CODE_TICK_OUT_2 ||
                        code == ERROR_CODE_TICK_OUT_3) {
                    return true;
                }
                return false;
            }

        }.nullSafe();
    }

}
